package com.jwm.power.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.zhxu.bs.MapSearcher;
import cn.zhxu.bs.util.MapUtils;
import com.jwm.power.mapper.QueryMapper;
import com.jwm.power.model.entity.*;
import com.jwm.power.util.Ut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//  公共查询类
@Controller
@RequestMapping("/com")
public class CommonController {
    @Autowired
    private QueryMapper queryMapper;

    @Autowired
    private MapSearcher mapSearcher;              // 注入 BeanSearcher 的检索器

    //  获取下拉选项数据
    @RequestMapping(value = "/querySelectDatas", method = RequestMethod.GET)
    public void QueryTreeSelect(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<String> cellList = new ArrayList<>();
            Map<String, Object> params = new HashMap<>();
            List<Map<String, Object>> searchResult = new ArrayList<>();
            Users users = (Users) StpUtil.getSession().get("users");
            String dataType = request.getParameter("dataType");
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("dataType", dataType);
            switch (dataType) {
                case "msgs":
                    objectMap.put("userid", users.getUserid());
                    break;
                case "menus":
                    objectMap.put("menutype", 0);
                    break;
                case "custype":
                    objectMap.put("dicname", "客户类型");
                    break;
                case "cusdis":
                    objectMap.put("dicname", "客户分布");
                    break;
                case "sources":
                case "cusSource":
                    objectMap.put("type", StpUtil.hasPermission("sources.type") ? 1 : 0);
                    break;
            }
            switch (dataType) {
                case "jobMenus":
                case "userMenus":
                    //  获取权限菜单
                    if (!users.getUsername().equals("admin")) {
                        Map<String, Object> queryMap = new HashMap<>();
                        queryMap.put("tName", "menus d");
                        queryMap.put("strCell", "d.menucode");
                        String strWhere = " left join JobMenus u on u.menucode = d.menucode ";
                        strWhere += " where u.jobcode='" + users.getJobcode() + "'";
                        queryMap.put("strWhere", strWhere);
                        cellList = queryMapper.queryCellDatas(queryMap);
                        if (cellList.size() == 0) {
                            cellList.add("-1");
                        }
                    }
                    params = MapUtils.builder()
                            .field("menucode", cellList).op("il")
                            .put("userid", request.getParameter("userid"))
                            .put("jobcode", request.getParameter("jobcode"))
                            .build();
                    searchResult = mapSearcher.searchAll(Menus.class, params);
                    break;
                case "depts":
                case "userDepts":
                case "noticDepts":
                case "orderTypeDepts":
                    //  获取权限部门
                    if (!users.getUsername().equals("admin")) {
                        Map<String, Object> queryMap = new HashMap<>();
                        queryMap.put("tName", "Depts d");
                        queryMap.put("strCell", "d.deptcode");
                        String strWhere = " left join UserDepts u on u.deptcode = d.deptcode ";
                        strWhere += " where u.userid=" + users.getUserid();
                        queryMap.put("strWhere", strWhere);
                        cellList = queryMapper.queryCellDatas(queryMap);
                        if (cellList.size() == 0) {
                            cellList.add(users.getDeptcode());
                        }
                    }
                    params = MapUtils.builder()
                            .field("deptcode", cellList).op("il")
                            .put("userid", request.getParameter("userid"))
                            .put("noticeid", request.getParameter("noticeid"))
                            .build();
                    searchResult = mapSearcher.searchAll(Depts.class, params);
                    break;
                case "jobs":
                case "userJobs":
                case "queryJobs":
                    searchResult = mapSearcher.searchAll(Deptjob.class, MapUtils.flat(request.getParameterMap()));
                    break;
                case "maketaskuser":
                    String deptname = request.getParameter("deptname");
 //                   System.out.println("deptcode是 " +deptname);
//                    if (!users.getUsername().equals("admin")) {
                        Map<String, Object> queryMap = new HashMap<>();
                        queryMap.put("tName", "users u");
                        queryMap.put("strCell", "u.name");
                        String strWhere = " JOIN Depts d ON u.deptcode = d.deptcode ";
                        strWhere += "WHERE d.deptname = '" + deptname + "'";
                        queryMap.put("strWhere", strWhere);
//                        System.out.println("数据库语句是： "+queryMap);
                        cellList = queryMapper.queryCellDatas(queryMap);
//                        System.out.println(cellList.getClass().getName()+ "," +cellList);
                        if (cellList.size() == 0) {
                            cellList.add("-1");
                        }
//                    }
                    params = MapUtils.builder()
                            .field("deptname", cellList).op("il")
                            .build();
                    searchResult = mapSearcher.searchAll(Depts.class, params);
                    break;
                default:
                    searchResult = queryMapper.querySelectDatas(objectMap);
                    break;
            }
            Ut.toJson(response, searchResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //  查询公告
    @RequestMapping(value = "/queryNotices", method = RequestMethod.GET)
    public void QueryNotices(HttpServletRequest request, HttpServletResponse response) {
        try {
            Users users = (Users) StpUtil.getSession().get("users");
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("strCell", " top 1 id, createdate, content_update");
            objectMap.put("tName", "Sys_Notices n ");
            String strWhere = " where exists ( select nid from Sys_NoticesUser un where un.nid =n.id and un.userid = " + users.getUserid() + " and un.rstatus=0 )";
            strWhere += " and date_validity > getdate() order by date_validity desc";
            objectMap.put("strWhere", strWhere);
            List<Map<String, Object>> searchResult = queryMapper.queryDefinedDatas(objectMap);
            Ut.toJson(response, searchResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
